/*
 * @Author: 1960116313@qq.com 1960116313@qq.com
 * @Date: 2023-10-23 14:13:58
 * @LastEditors: 1960116313@qq.com 1960116313@qq.com
 * @LastEditTime: 2023-10-24 17:07:53
 * @FilePath: \panuniapp\src\utils\request.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { getToken } from "@/utils/auth";
import { msg } from "@/install/msg";
import { navReLaunch } from "@/install/navigate";
import website from "@/config/website";
import { Base64 } from "js-base64";

// 返回登录页面
const loginOut = () => {
  navReLaunch("/pages/login/login");
};

// 定义请求地址
const BASE_URL = `${import.meta.env.VITE_APP_BASE_URL || ""}`;

const getHeaders = () => {
  const header = {
    "Hulk-Auth": "bearer " + getToken(),
    "Content-Type": "application/x-www-form-urlencoded",
    Authorization: `Basic ${Base64.encode(
      `${website.clientId}:${website.clientSecret}`
    )}`,
  };
  return header;
};
// 加载动画
let loadingCount = 0; // 记录当前显示的 loading 数量
const showLoading = () => {
  if (loadingCount === 0) {
    uni.showLoading({
      title: "加载中...",
      mask: true,
    });
  }
  loadingCount++;
};
const hideLoading = () => {
  loadingCount > 0 && loadingCount--;
  if (loadingCount <= 0) {
    uni.hideLoading();
  }
};

// 网络请求
const request = (obj, timeout = 6000) => {
  obj.url = obj.url ?? ""; // 请求路径
  obj.method = obj.method ?? "GET"; //请求方式
  obj.params = obj.params ?? {}; //请求携带的数据
  obj.header = getHeaders(); //请求头信息 content-type
  obj.requestTime = obj.requestTime ?? 200; //多久时间内完成网络请求，则不显示提示框
  obj.loading, //是否显示请求加载中
    //定义网络请求如果在一定时间内没有完成，则显示加载中提示框，默认时间为：500毫秒
    setTimeout(() => {
      if (obj.loading) {
        showLoading();
      }
    }, obj.requestTime);

  return new Promise((resolve, reject) => {
    uni.request({
      url: BASE_URL + obj.url,
      method: obj.method,
      data: obj.params,
      header: obj.header,
      success: (res) => {
        switch (res.data.code || res.statusCode) {
          case 200:
            resolve(res);
            break;
          case 401:
            msg("账号已过期，需要重新登录", () => {
              loginOut();
            });
            break;
          case 404:
            msg("接口不存在");
            break;
          default:
            msg("服务器异常！");
            break;
        }
        reject(res);
      },
      fail: (err) => {
        msg("服务器异常,请检查网络是否正常");
        reject(err);
      },
      complete: () => {
        if (obj.loading) {
          hideLoading();
        }
      },
    });
  });
};

export default request;
